/*
XPTracker - Alternative Agile Tracking Tool
Copyright (C) 2006-2009  Stephen Starkey

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
*/
package org.xptracker.persistence.hibernate;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.CollectionOfElements;
import org.xptracker.persistence.Identified;
import org.xptracker.persistence.ObjectChange;

import javax.persistence.*;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * This class is no longer used.  It exists solely for migration purposes.
 * @see org.xptracker.domain.StoryState
 */
@Entity
@Table(name="object_changes")
@Cache(usage=CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
@Deprecated
public class HibernateObjectChange extends Identified implements ObjectChange {
  private String itemId;
  private Date occurred = new Date();
  private Date stopDate;
  private Map<String, String> fieldMap = new HashMap<String, String>();

  protected HibernateObjectChange() {
  }

  @CollectionOfElements(fetch=FetchType.EAGER)
  @JoinTable(name="object_change_fields")
  protected Map<String, String> getFieldMap() {
    return fieldMap;
  }

  protected void setFieldMap(Map<String, String> fieldMap) {
    this.fieldMap = fieldMap;
  }

  @Column(name="item_id")
  public String getItemId() {
    return itemId;
  }

  public void setItemId(String itemId) {
    this.itemId = itemId;
  }

  @Transient
  public Collection<String> listFields() {
    return fieldMap.keySet();
  }

  @Transient
  public String getValue(String fieldName) {
    return fieldMap.get(fieldName);
  }

  @Column(name="start_date")
  public Date getOccurred() {
    return occurred;
  }

  public void setOccurred(Date occurred) {
    this.occurred = occurred;
  }

  @Column(name="stop_date")
  public Date getStopDate() {
    return stopDate;
  }

  public void setStopDate(Date stopDate) {
    this.stopDate = stopDate;
  }

  public String toString() {
    return new StringBuilder("Change ").append(getId()).append("@[").append(occurred)
        .append(" - ").append(stopDate)
        .append("] on item ").append(itemId)
        .append(": ").append(fieldMap.toString()).toString();
  }
}
